home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / system / pgraf130.zip / C.ZIP / PGRAFBUF.C < prev    next >
C/C++ Source or Header  |  1991-10-13  |  5KB  |  187 lines

  1. /* File pgrafbuf.c */
  2. /*---------------------------------------------------------------------------*
  3.  |
  4.  |    Copyright (c) F van der Hulst 1991
  5.  |    All Rights Reserved.
  6.  |
  7.  |     Revisions:
  8.  |     26 June    1991: Added _p_getbyte() and _p_putbyte() to support model independence
  9.  |  10 October 1991: Virtual memory added by Dieter Gugeler
  10.  *
  11.  *---------------------------------------------------------------------------*/
  12.  
  13. #ifdef __TURBOC__
  14. #include <alloc.h>
  15. #else
  16. #include <sys\types.h>
  17. #include <malloc.h>
  18. #endif
  19.  
  20. #include <stdio.h>
  21. #include <io.h>
  22. #include <fcntl.h>
  23. #include <sys\stat.h>
  24. #include "pgraph.h"
  25.  
  26. #define    TRUE                1
  27. #define    FALSE                0
  28.  
  29. #define    HEAPMEMLEFT        2048        /* Leave 2 KBYTE free on the heap */
  30. #define    MIN_BLOCK_SIZE    512        /* Minimum block size */
  31. #define    PRTTEMPFILE        "prt001.$$$"
  32.  
  33. static    char huge *        far_buffer;
  34.  
  35. #ifdef VIRT_MEM
  36. static    char *            near_buffer;
  37. static    unsigned int    block_size;
  38. static    unsigned int    curr_block;
  39. static    int                handle;
  40. static    char                disk_buffer;
  41. static    char                buffer_changed;
  42.  
  43. static int pascal near initvdsk(unsigned long int size)
  44. {
  45.     unsigned int num_blocks;
  46.     unsigned int ll;
  47.     unsigned long int free_space;
  48.  
  49.     free_space = farcoreleft() - HEAPMEMLEFT;       /* Leave room for other graph memory uses */
  50.     if (size > free_space) {
  51.         free_space = coreleft() - HEAPMEMLEFT;       /* Can't do it on far heap -- use near heap */
  52.         if (free_space > 0xff00) block_size = 0xff00;
  53.         else block_size = (unsigned int) free_space;
  54.         if (block_size < MIN_BLOCK_SIZE) return FALSE;
  55.         num_blocks = (unsigned int) ((size + block_size - 1) / block_size);
  56.         printf("Allocating %ud disk buffers of 0x%x bytes\n", num_blocks, block_size);
  57.  
  58.         remove(PRTTEMPFILE);
  59.         if ((handle = open(PRTTEMPFILE, O_CREAT | O_BINARY | O_TRUNC, S_IREAD | S_IWRITE)) == -1)
  60.             return FALSE;
  61.  
  62.         if ((near_buffer = malloc(block_size)) == NULL) return FALSE;
  63.         for (ll=0; ll <= num_blocks; ll++)
  64.             if ((write(handle, near_buffer, block_size)) == -1)    return FALSE;
  65.  
  66.         curr_block = 0;
  67.         disk_buffer = TRUE;
  68.     } else far_buffer = farmalloc(size);
  69.     return TRUE;
  70. }
  71.  
  72. static void pascal near closevdsk(void)
  73. {
  74.     close(handle);
  75.     remove(PRTTEMPFILE);
  76.     disk_buffer = FALSE;
  77. }
  78.  
  79. static void pascal near writevdsk(unsigned int blocknum)
  80. {
  81.     lseek(handle, (long)block_size * blocknum, SEEK_SET);
  82.     write(handle, near_buffer, block_size);
  83.     buffer_changed = FALSE;
  84. }
  85.  
  86. static void pascal near readvdsk(unsigned int blocknum)
  87. {
  88.     if (buffer_changed)        writevdsk(curr_block);
  89.  
  90.     lseek(handle, (long)block_size * blocknum, SEEK_SET);
  91.     read(handle, near_buffer, block_size);
  92.     curr_block = blocknum;
  93. }
  94. #endif
  95.  
  96. static unsigned char FUNC_TYPE pgr_getbyte(unsigned long int offset)
  97. {
  98. #ifdef VIRT_MEM
  99.     unsigned int blocknum;
  100.  
  101.     if (disk_buffer) {
  102.         if ((blocknum = (unsigned) (offset / block_size)) != curr_block)
  103.             readvdsk(blocknum);
  104.         return near_buffer[(unsigned int)(offset-(long)blocknum*block_size)];
  105.     }
  106. #endif
  107.     return far_buffer[offset];
  108. }
  109.  
  110.  
  111. static void FUNC_TYPE pgr_putbyte(unsigned long int offset, unsigned char byte)
  112. {
  113. #ifdef VIRT_MEM
  114.     unsigned int blocknum;
  115.  
  116.     if (disk_buffer) {
  117.         if ((blocknum = (unsigned) (offset / block_size)) != curr_block)
  118.             readvdsk(blocknum);
  119.         near_buffer[(unsigned int)(offset-((long)blocknum * block_size))] = byte;
  120.         buffer_changed = TRUE;
  121.         return;
  122.     }
  123. #endif
  124.     far_buffer[offset] = byte;
  125. }
  126.  
  127. #if defined(__TURBOC__)
  128. /* The following two routines are required in any 64K Code model, since
  129.     _p_graphgetmem and _p_graphfreemem MUST point to a HUGE function, and
  130.     in small code model libraries, these functions are NEAR */
  131.  
  132. static void FUNC_TYPE pgr_graphfreemem(void far *ptr, unsigned long size)
  133. {
  134.     farfree(ptr);
  135. #pragma warn -par
  136. }
  137. #pragma warn .par
  138.  
  139. static void far * FUNC_TYPE pgr_graphgetmem(unsigned long size)
  140. {
  141.     return farmalloc(size);
  142. }
  143. #else
  144. static void far * FUNC_TYPE pgr_graphgetmem(unsigned long size)
  145. {
  146.     return halloc(size, 1);
  147. }
  148.  
  149. static void FUNC_TYPE pgr_graphfreemem(void far *ptr, unsigned long size)
  150. {
  151.     hfree(ptr);
  152. }
  153. #endif
  154.  
  155. static int FUNC_TYPE pgr_graphgetbuff(unsigned long size)
  156. {
  157. #ifdef VIRT_MEM
  158.     return initvdsk(size);
  159. #else
  160.     return (far_buffer = pgr_graphgetmem(size)) != NULL;
  161. #endif
  162. }
  163.  
  164. static void FUNC_TYPE pgr_graphfreebuff(unsigned long size)
  165. {
  166. #ifdef VIRT_MEM
  167.     if (disk_buffer) {
  168.         closevdsk();
  169.         free(near_buffer);
  170.     } else pgr_graphfreemem((void far *)far_buffer, size);
  171. #else
  172.     pgr_graphfreemem((void far *)far_buffer, size);
  173. #endif
  174. }
  175.  
  176. void init_buffering(void)
  177. {
  178.     _p_graphgetmem     = pgr_graphgetmem;
  179.     _p_graphfreemem     = pgr_graphfreemem;
  180.  
  181.     _p_graphgetbuff     = pgr_graphgetbuff;
  182.     _p_graphfreebuff     = pgr_graphfreebuff;
  183.  
  184.     _p_putbyte             = pgr_putbyte;
  185.     _p_getbyte             = pgr_getbyte;
  186. }
  187.